package rpc.core;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rpc.bootstrap.springboot.Bootstrap;
import rpc.utils.InitCloseUtils;

/*********
 *
 *
 *@author 197
 *@date 2020/6/18 20:45
 *
 **/


public class RpcApp implements InitClose, StartStop {

  private static final Logger logger = LoggerFactory.getLogger(RpcApp.class);
  private final String appName;
  private final List<Object> rpcNeedObjects = new ArrayList<>();

  private final Bootstrap bootstrap;

  public RpcApp(String appName, Bootstrap bootstrap) {
    this.appName = appName;
    this.bootstrap = bootstrap;
  }

  @Override
  public void init() {
    this.rpcNeedObjects.addAll(bootstrap.getNettyServerList());
    this.rpcNeedObjects.addAll(bootstrap.getSelfCheckHttpServerList());
    this.rpcNeedObjects.addAll(bootstrap.getServiceBeanList());
    this.rpcNeedObjects.addAll(bootstrap.getRefererBeanList());
    this.rpcNeedObjects.addAll(bootstrap.getNettyClientList());
    this.rpcNeedObjects.add(bootstrap.getHttpServer());
    InitCloseUtils.init(rpcNeedObjects);
    logger.info("RpcApp init end");
  }

  @Override
  public void start() {
    InitCloseUtils.start(rpcNeedObjects);
    logger.info("start end");
  }

  @Override
  public void close() {
    InitCloseUtils.close(rpcNeedObjects);
  }

  @Override
  public void stop() {
    InitCloseUtils.stop(rpcNeedObjects);
  }

  public String getAppName() {
    return appName;
  }


  public Bootstrap getBootstrap() {
    return bootstrap;
  }

  public void shutdown() {
    bootstrap.getNettyClientExecutorService().shutdown();
  }
}
